// import common api
import org.pi.common.Api._

// load input image
val a = imread("data/4Spiele.png")

// ----------------
// compute gradient
// and other stuff
// ----------------
tic

val b = rgb2gray(a)
val (fgx, fgy) = gradient(b)
val fg = abs(fgx,fgy)
val fgBlurred = hanningBlur(fg,9)
val fa = angle(fgx,fgy)

toc("ImageGradient") 

var c = zeros(fa.length(0),fa.length(1),3)
c(~~,~~,0).copyValuesFrom( (fa+pi)/(2*pi) )
c(~~,~~,1) = 1
c(~~,~~,2).copyValuesFrom(fg)

// ----------------
// output
// ----------------

figure
// show output image
subplot(2,2,0);
imshow( hsv2rgb(c) )

// show gradient
subplot(2,2,1);
imshow( fg*(-1.0f) )
plot( 0.0f::size(fg,1).toFloat::Nil, (size(fg,0)/2).toFloat::(size(fg,0)/2).toFloat::Nil)

// show center line of image as 1d plot
subplot(2,2,2)
plot( fg(size(fg,0)/2, ~~) )
plot( fgBlurred(size(fgBlurred,0)/2, ~~), "color", java.awt.Color.red )
gca.preserveAspectRatio = org.pi.common.plot.axes.AspectRatio.Free

subplot(2,2,3)
imshow( fgBlurred*(-1.0f) )
plot( 0.0f::size(fgBlurred,1).toFloat::Nil, (size(fgBlurred,0)/2).toFloat::(size(fgBlurred,0)/2).toFloat::Nil, "color", java.awt.Color.red)

